package servlet.member;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.control.MailHelper;
import bean.control.MemberMgr;

@SuppressWarnings("serial")
public class RegisterMemberServlet extends HttpServlet {
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.getRequestDispatcher("./member/registerMember.jsp").forward(req, resp);
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String stuNum = req.getParameter("stuNum");
		String stuName = req.getParameter("stuName");
		String pass = req.getParameter("pass");
		PrintWriter out = resp.getWriter();
		
		try {
			long memId = MemberMgr.addMember(stuNum, stuName, pass);
			
			if (memId == 0)
				out.print("0");
			else {
				MailHelper.sendMemberConfirmMail(stuNum, stuName, memId);
				out.print("1");
			}
		} finally {
			out.close();
		}
	}
}
